﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PluginCommon;
using WinAPI.User32;

namespace EnableWindows
{
    public partial class MainUserControl : PluginFormBase
    {
        public MainUserControl()
        {
            int x = User32Dll.RegisterHotKey(Handle, 0x33, User32Dll.MOD_CONTROL, (uint)0x6A);
            InitializeComponent();
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0x312)
            {
                if (m.WParam.ToInt32() == 0x33)
                {
                    EnableTargetWindow();
                }
            }
        }

        public override void HandleMainProgramMessage(MainProgramMessage message)
        {
            if (message.Type == MainProgramMessageType.HotKey)
            {
                if ((int)(message.Data["HotKeyId"]) == 0x33)
                {
                    EnableTargetWindow();
                }
            }
        }

        public void EnableTargetWindow()
        {
            POINT pos = new POINT();
            User32Dll.GetCursorPos(ref pos);
            IntPtr handle = User32Dll.WindowFromPoint(pos);
            User32Dll.ScreenToClient(handle, ref pos);
            IntPtr target = User32Dll.ChildWindowFromPoint(handle, pos);
            User32Dll.EnableWindow(target, 1);
        }

    }
}
